package com.mapapp.flutter_mapapp.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mapapp.flutter_mapapp.R;
import com.mapapp.flutter_mapapp.adapter.StepFloorAdapter;
import com.mapapp.flutter_mapapp.adapter.StepGrossAdapter;
import com.mapapp.flutter_mapapp.application.Constants;
import com.mapapp.flutter_mapapp.application.MyApplication;
import com.mapapp.flutter_mapapp.bean.HomePageShouldBeRefreshed;
import com.mapapp.flutter_mapapp.bean.StepFloorBean;
import com.mapapp.flutter_mapapp.bean.StepFloorProcessBean;
import com.mapapp.flutter_mapapp.bean.WaitForUploading;
import com.mapapp.flutter_mapapp.bean.WaitForUploadingDao;
import com.mapapp.flutter_mapapp.http.Url;
import com.mapapp.flutter_mapapp.persenter.StepListPersenter;
import com.mapapp.flutter_mapapp.util.MyActivityManager;
import com.mapapp.flutter_mapapp.util.RxBus;
import com.mapapp.flutter_mapapp.util.SharePreferenceUtil;
import com.mapapp.flutter_mapapp.view.StepListViewInterface;
import com.mapapp.flutter_mapapp.widget.BetterGesturesRecyclerView;
import com.mapapp.flutter_mapapp.widget.StepFloorDetailDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Observable;
import rx.functions.Action1;

public class StepListActivity extends ImmersiveBaseActivity implements View.OnClickListener, StepListViewInterface {
    private static final String TAG = "StepListActivity";

    @BindView(R.id.rv_step_floor)
    BetterGesturesRecyclerView rvStepFloor;
    @BindView(R.id.iv_step_list_back)
    ImageView ivStepListBack;
    @BindView(R.id.iv_step_list_title)
    TextView ivStepListTitle;
    @BindView(R.id.rv_step_gross)
    RecyclerView rvStepGross;
    @BindView(R.id.ll_step_has_date)
    LinearLayout llStepHasDate;
    @BindView(R.id.ll_step_no_date)
    LinearLayout llStepNoDate;

    private List<StepFloorProcessBean> mGrosses = new ArrayList<>();
    private StepGrossAdapter mGrossAdapter;
    private StepFloorAdapter mFloorAdapter;
    private ArrayList<StepFloorBean> mFloorInfos = new ArrayList<>();
    private StepListPersenter mPersenter;
    private String mParentId;
    private StepFloorDetailDialog mDialog;
    private Observable<HomePageShouldBeRefreshed> observable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_step_list);
        ButterKnife.bind(this);

        mContext = this;
        mPersenter = new StepListPersenter(this, this, lifecycleSubject);
        init();
        //mParentId = "0015771568995480045400155d4c6010";
        mPersenter.getListInfo(mParentId);
        observable = RxBus.get().register("refresh",HomePageShouldBeRefreshed.class);
        observable.subscribe(new Action1<HomePageShouldBeRefreshed>() {
            @Override
            public void call(HomePageShouldBeRefreshed homePageShouldBeRefreshed) {
                Log.i(TAG,"observable HomePageShouldBeRefreshed");
                if(mPersenter != null){
                    mPersenter.getListInfo(mParentId);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_step_list_back:
                MyActivityManager.getActivityManager().pop(this);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.get().unregister("refresh",observable);
    }

    private void init() {
        mParentId = getIntent().getStringExtra("parentId");
        initGrossList();
        initFloorList();
        mDialog = new StepFloorDetailDialog(this);

        ivStepListBack.setOnClickListener(this);
    }

    private void initGrossList() {
        mGrossAdapter = new StepGrossAdapter(mGrosses, this);
        LinearLayoutManager llmanage = new LinearLayoutManager(mContext);
        llmanage.setOrientation(LinearLayoutManager.HORIZONTAL);
        rvStepGross.setLayoutManager(llmanage);
//        rvStepGross.addItemDecoration(new HorizontalSpaceItemDecoration(60));
        rvStepGross.setAdapter(mGrossAdapter);
    }

    private void initFloorList() {
        mFloorAdapter = new StepFloorAdapter(mFloorInfos, this);
        LinearLayoutManager llmanage = new LinearLayoutManager(mContext);
        rvStepFloor.setLayoutManager(llmanage);
        rvStepFloor.setAdapter(mFloorAdapter);
        mFloorAdapter.setOnItemClickListener(new StepFloorAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                mGrosses = mFloorInfos.get(position).getProcesses();
                mGrossAdapter.notify(mGrosses);
                ivStepListTitle.setText(mFloorInfos.get(position).getRegionName());
            }

            @Override
            public void onNextClick(int position) {
                showMyDialog(position);
            }
        });
    }

    private void showMyDialog(int position) {
        if (!mDialog.isShowing()) {
            Window dialogWindow = mDialog.getWindow();
            dialogWindow.setWindowAnimations(R.style.pickPhoto_dialogAnim);
            dialogWindow.setGravity(Gravity.BOTTOM);
            mDialog.show();
            mDialog.setmStepBean(mFloorInfos.get(position));
        }
    }

    @Override
    public void showData(StepFloorBean data) {
        if (data.getProcesses().size() > 0) {
            ivStepListTitle.setText(data.getRegionName());
            mGrosses = data.getProcesses();
            mGrossAdapter.notify(mGrosses);
            mFloorInfos = data.getRegions();
            for(int i = 0 ; i < mFloorInfos.size() ; i ++){
                for(int j = 0 ; j < mFloorInfos.get(i).getRegions().size() ; j ++){
                    /*if(mFloorInfos.get(i).getRegions().get(j).getRegionName().equals("0105")){
                        mFloorInfos.get(i).getRegions().get(j).setDbFlag(1);
                    }*/
                    WaitForUploading unique = MyApplication.getInstance().getDaoSession().getWaitForUploadingDao()
                            .queryBuilder().where(WaitForUploadingDao.Properties.RegionId.eq(mFloorInfos.get(i).getRegions().get(j).getRegionId()),
                                    WaitForUploadingDao.Properties.HostAddress.eq(Url.BASE_URL),
                                    WaitForUploadingDao.Properties.UserId.eq(SharePreferenceUtil.get(mContext, Constants.USERNAME, ""))).unique();
                    if (unique != null) {
                        mFloorInfos.get(i).getRegions().get(j).setDbFlag(1);
                    }
                }
            }
            mFloorAdapter.notify(mFloorInfos);
        } else {
            llStepHasDate.setVisibility(View.GONE);
            llStepNoDate.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void noData() {
        llStepHasDate.setVisibility(View.GONE);
        llStepNoDate.setVisibility(View.VISIBLE);
    }

}
